using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;


namespace SIS.DAL
{
    /// <summary>
    /// The SchoolProfileTableDB class is responsible for interacting with the database to retrieve and store information
    /// about SchoolProfileTable objects.
    /// </summary>
    public class SchoolProfileTableDB
    {
        #region "Public Methods"

        /// <summary>
        /// Gets an instance of SchoolProfileTable from the underlying datasource.
        /// </summary>
        /// <param name="schoolKey">The unique SchoolKey of the SchoolProfileTable in the database.</param>
        /// <returns>An SchoolProfileTable when the SchoolKey was found in the database, or null otherwise.</returns>
        public static SchoolProfileTable GetItem(int schoolKey)
        {
            SchoolProfileTable mySchoolProfileTable = null;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocSchoolProfileTableSelectSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;
                    myCommand.Parameters.AddWithValue("@schoolKey", schoolKey);

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.Read())
                        {
                            mySchoolProfileTable = FillDataRecord(myReader);
                        }
                        myReader.Close();
                    }
                    myConnection.Close();
                }
                return mySchoolProfileTable;
            }
        }

        /// <summary>
        /// Returns a list with SchoolProfileTable objects.
        /// </summary>
        /// <returns>A generics List with the SchoolProfileTable objects.</returns>
        public static SchoolProfileTableList GetList()
        {
            SchoolProfileTableList tempList = new SchoolProfileTableList();
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocSchoolProfileTableSelectList", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myConnection.Open();
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.HasRows)
                        {
                            while (myReader.Read())
                            {
                                tempList.Add(FillDataRecord(myReader));
                            }
                        }
                        myReader.Close();
                    }
                }
            }
            return tempList;
        }

        /// <summary>
        /// Saves a SchoolProfileTable in the database.
        /// </summary>
        /// <param name="mySchoolProfileTable">The SchoolProfileTable instance to save.</param>
        /// <returns>The new SchoolKey if the SchoolProfileTable is new in the database or the existing SchoolKey when an item was updated.</returns>
        public static int Save(SchoolProfileTable mySchoolProfileTable)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocSchoolProfileTableInsertUpdateSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    if (mySchoolProfileTable.SchoolKey == -1)
                    {
                        myCommand.Parameters.AddWithValue("@schoolKey", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@schoolKey", mySchoolProfileTable.SchoolKey);
                    }
                    if (string.IsNullOrEmpty(mySchoolProfileTable.SchoolShortName))
                    {
                        myCommand.Parameters.AddWithValue("@schoolShortName", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@schoolShortName", mySchoolProfileTable.SchoolShortName);
                    }
                    if (string.IsNullOrEmpty(mySchoolProfileTable.SchoolName))
                    {
                        myCommand.Parameters.AddWithValue("@schoolName", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@schoolName", mySchoolProfileTable.SchoolName);
                    }
                    if (string.IsNullOrEmpty(mySchoolProfileTable.SchoolOtherName))
                    {
                        myCommand.Parameters.AddWithValue("@schoolOtherName", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@schoolOtherName", mySchoolProfileTable.SchoolOtherName);
                    }
                    if (string.IsNullOrEmpty(mySchoolProfileTable.SchoolBrief))
                    {
                        myCommand.Parameters.AddWithValue("@schoolBrief", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@schoolBrief", mySchoolProfileTable.SchoolBrief);
                    }
                    if (string.IsNullOrEmpty(mySchoolProfileTable.SchoolOtherBrief))
                    {
                        myCommand.Parameters.AddWithValue("@schoolOtherBrief", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@schoolOtherBrief", mySchoolProfileTable.SchoolOtherBrief);
                    }
                    if (mySchoolProfileTable.RegisterYear == null)
                    {
                        myCommand.Parameters.AddWithValue("@registerYear", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@registerYear", mySchoolProfileTable.RegisterYear);
                    }
                    if (mySchoolProfileTable.OpenYear == null)
                    {
                        myCommand.Parameters.AddWithValue("@openYear", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@openYear", mySchoolProfileTable.OpenYear);
                    }
                    if (string.IsNullOrEmpty(mySchoolProfileTable.OwnerName))
                    {
                        myCommand.Parameters.AddWithValue("@ownerName", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@ownerName", mySchoolProfileTable.OwnerName);
                    }
                    if (string.IsNullOrEmpty(mySchoolProfileTable.HeadMasterName))
                    {
                        myCommand.Parameters.AddWithValue("@headMasterName", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@headMasterName", mySchoolProfileTable.HeadMasterName);
                    }
                    if (string.IsNullOrEmpty(mySchoolProfileTable.RegistrarName))
                    {
                        myCommand.Parameters.AddWithValue("@registrarName", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@registrarName", mySchoolProfileTable.RegistrarName);
                    }
                    if (string.IsNullOrEmpty(mySchoolProfileTable.RegistrationNumber))
                    {
                        myCommand.Parameters.AddWithValue("@registrationNumber", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@registrationNumber", mySchoolProfileTable.RegistrationNumber);
                    }
                    if (string.IsNullOrEmpty(mySchoolProfileTable.WebsiteUrl))
                    {
                        myCommand.Parameters.AddWithValue("@websiteUrl", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@websiteUrl", mySchoolProfileTable.WebsiteUrl);
                    }
                    if (string.IsNullOrEmpty(mySchoolProfileTable.EmailAddress))
                    {
                        myCommand.Parameters.AddWithValue("@emailAddress", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@emailAddress", mySchoolProfileTable.EmailAddress);
                    }
                    if (string.IsNullOrEmpty(mySchoolProfileTable.FaxNumber))
                    {
                        myCommand.Parameters.AddWithValue("@faxNumber", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@faxNumber", mySchoolProfileTable.FaxNumber);
                    }
                    if (string.IsNullOrEmpty(mySchoolProfileTable.ContactNumber))
                    {
                        myCommand.Parameters.AddWithValue("@contactNumber", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@contactNumber", mySchoolProfileTable.ContactNumber);
                    }
                    if (string.IsNullOrEmpty(mySchoolProfileTable.Remark))
                    {
                        myCommand.Parameters.AddWithValue("@remark", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@remark", mySchoolProfileTable.Remark);
                    }
                    if (mySchoolProfileTable.Status == null)
                    {
                        myCommand.Parameters.AddWithValue("@status", DBNull.Value);
                    }
                    else
                    {
                        myCommand.Parameters.AddWithValue("@status", mySchoolProfileTable.Status);
                    }

                    //if (mySchoolProfileTable.CreatedDateTime == null)
                    //{
                    //    myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
                    //}
                    //else
                    //{
                    //    myCommand.Parameters.AddWithValue("@createdDateTime", mySchoolProfileTable.CreatedDateTime);
                    //}
                    //if (mySchoolProfileTable.ModifiedDateTime == null)
                    //{
                    //    myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
                    //}
                    //else
                    //{
                    //    myCommand.Parameters.AddWithValue("@modifiedDateTime", mySchoolProfileTable.ModifiedDateTime);
                    //}

                    DbParameter returnValue;
                    returnValue = myCommand.CreateParameter();
                    returnValue.Direction = ParameterDirection.ReturnValue;
                    myCommand.Parameters.Add(returnValue);

                    myConnection.Open();
                    myCommand.ExecuteNonQuery();
                    result = Convert.ToInt32(returnValue.Value);
                    myConnection.Close();
                }
            }
            return result;
        }

        /// <summary>
        /// Deletes a SchoolProfileTable from the database.
        /// </summary>
        /// <param name="schoolKey">The SchoolKey of the SchoolProfileTable to delete.</param>
        /// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
        public static bool Delete(int schoolKey)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocSchoolProfileTableDeleteSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myCommand.Parameters.AddWithValue("@schoolKey", schoolKey);
                    myConnection.Open();
                    result = myCommand.ExecuteNonQuery();
                    myConnection.Close();
                }
            }
            return result > 0;
        }

        /// <summary>
        /// Deletes a SchoolProfileTable from the database.
        /// </summary>
        /// <param name="schoolKey">The SchoolKey of the SchoolProfileTable to delete.</param>
        /// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
        public static bool UpdateLogo(int schoolKey, Byte[] imgLogo)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocSchoolProfileTableUpdateLogoSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myCommand.Parameters.AddWithValue("@schoolKey", schoolKey);
                    myCommand.Parameters.AddWithValue("@schoolLogo", imgLogo);
                    myConnection.Open();
                    result = myCommand.ExecuteNonQuery();
                    myConnection.Close();
                }
            }
            return result > 0;
        }

        /// <summary>
        /// Deletes a SchoolProfileTable from the database.
        /// </summary>
        /// <param name="schoolKey">The SchoolKey of the SchoolProfileTable to delete.</param>
        /// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
        public static bool UpdatePhoto(int schoolKey, Byte[] imgPhoto)
        {
            int result = 0;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocSchoolProfileTableUpdatePhotoSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myCommand.Parameters.AddWithValue("@schoolKey", schoolKey);
                    myCommand.Parameters.AddWithValue("@schoolPhoto", imgPhoto);
                    myConnection.Open();
                    result = myCommand.ExecuteNonQuery();
                    myConnection.Close();
                }
            }
            return result > 0;
        }

        /// <summary>
        /// Get Photo a SchoolProfileTable from the database.
        /// </summary>
        /// <param name="schoolKey">The SchoolKey of the SchoolProfileTable to delete.</param>
        /// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
        public static Byte[] GetPhoto(int schoolKey)
        {
            Byte[] imgPhoto = null;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocSchoolProfileTableSelectPhotoSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myCommand.Parameters.AddWithValue("@schoolKey", schoolKey);
                    myConnection.Open();
                    myCommand.CommandType = CommandType.StoredProcedure;
                    
                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.HasRows)
                        {
                            while (myReader.Read())
                            {
                                imgPhoto = (Byte[])myReader["SchoolPhoto"];
                            }
                        }
                        myReader.Close();
                    }
                    myConnection.Close();
                }
            }
            return imgPhoto;
        }

        /// <summary>
        /// Get Photo a SchoolProfileTable from the database.
        /// </summary>
        /// <param name="schoolKey">The SchoolKey of the SchoolProfileTable to delete.</param>
        /// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
        public static Byte[] GetLogo(int schoolKey)
        {
            Byte[] imgPhoto = null;
            using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
            {
                using (SqlCommand myCommand = new SqlCommand("sprocSchoolProfileTableSelectLogoSingleItem", myConnection))
                {
                    myCommand.CommandType = CommandType.StoredProcedure;

                    myCommand.Parameters.AddWithValue("@schoolKey", schoolKey);
                    myConnection.Open();
                    myCommand.CommandType = CommandType.StoredProcedure;

                    using (SqlDataReader myReader = myCommand.ExecuteReader())
                    {
                        if (myReader.HasRows)
                        {
                            while (myReader.Read())
                            {
                                imgPhoto = (Byte[])myReader["SchoolLogo"];
                            }
                        }
                        myReader.Close();
                    }
                    myConnection.Close();
                }
            }

            return imgPhoto;
        }

        #endregion

        /// <summary>
        /// Initializes a new instance of the SchoolProfileTable class and fills it with the data fom the IDataRecord.
        /// </summary>
        private static SchoolProfileTable FillDataRecord(IDataRecord myDataRecord)
        {
            SchoolProfileTable mySchoolProfileTable = new SchoolProfileTable();
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("SchoolKey")))
            {
                mySchoolProfileTable.SchoolKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("SchoolKey"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("SchoolShortName")))
            {
                mySchoolProfileTable.SchoolShortName = myDataRecord.GetString(myDataRecord.GetOrdinal("SchoolShortName"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("SchoolName")))
            {
                mySchoolProfileTable.SchoolName = myDataRecord.GetString(myDataRecord.GetOrdinal("SchoolName"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("SchoolOtherName")))
            {
                mySchoolProfileTable.SchoolOtherName = myDataRecord.GetString(myDataRecord.GetOrdinal("SchoolOtherName"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("SchoolBrief")))
            {
                mySchoolProfileTable.SchoolBrief = myDataRecord.GetString(myDataRecord.GetOrdinal("SchoolBrief"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("SchoolOtherBrief")))
            {
                mySchoolProfileTable.SchoolOtherBrief = myDataRecord.GetString(myDataRecord.GetOrdinal("SchoolOtherBrief"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("RegisterYear")))
            {
                mySchoolProfileTable.RegisterYear = myDataRecord.GetInt16(myDataRecord.GetOrdinal("RegisterYear"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("OpenYear")))
            {
                mySchoolProfileTable.OpenYear = myDataRecord.GetInt16(myDataRecord.GetOrdinal("OpenYear"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("OwnerName")))
            {
                mySchoolProfileTable.OwnerName = myDataRecord.GetString(myDataRecord.GetOrdinal("OwnerName"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("SchoolForeColor")))
            {
                mySchoolProfileTable.SchoolForeColor = myDataRecord.GetString(myDataRecord.GetOrdinal("SchoolForeColor"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("SchoolBackColor")))
            {
                mySchoolProfileTable.SchoolBackColor = myDataRecord.GetString(myDataRecord.GetOrdinal("SchoolBackColor"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("SchoolLogo")))
            {
                mySchoolProfileTable.SchoolLogo = (byte[])myDataRecord.GetValue(myDataRecord.GetOrdinal("SchoolLogo"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("SchoolPhoto")))
            {
                mySchoolProfileTable.SchoolPhoto = (byte[])myDataRecord.GetValue(myDataRecord.GetOrdinal("SchoolPhoto"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("HeadMasterName")))
            {
                mySchoolProfileTable.HeadMasterName = myDataRecord.GetString(myDataRecord.GetOrdinal("HeadMasterName"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("RegistrarName")))
            {
                mySchoolProfileTable.RegistrarName = myDataRecord.GetString(myDataRecord.GetOrdinal("RegistrarName"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("RegistrationNumber")))
            {
                mySchoolProfileTable.RegistrationNumber = myDataRecord.GetString(myDataRecord.GetOrdinal("RegistrationNumber"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("WebsiteUrl")))
            {
                mySchoolProfileTable.WebsiteUrl = myDataRecord.GetString(myDataRecord.GetOrdinal("WebsiteUrl"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("EmailAddress")))
            {
                mySchoolProfileTable.EmailAddress = myDataRecord.GetString(myDataRecord.GetOrdinal("EmailAddress"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("FaxNumber")))
            {
                mySchoolProfileTable.FaxNumber = myDataRecord.GetString(myDataRecord.GetOrdinal("FaxNumber"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ContactNumber")))
            {
                mySchoolProfileTable.ContactNumber = myDataRecord.GetString(myDataRecord.GetOrdinal("ContactNumber"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Remark")))
            {
                mySchoolProfileTable.Remark = myDataRecord.GetString(myDataRecord.GetOrdinal("Remark"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
            {
                mySchoolProfileTable.Status = myDataRecord.GetInt16(myDataRecord.GetOrdinal("Status"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
            {
                mySchoolProfileTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
            }
            if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
            {
                mySchoolProfileTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
            }
            return mySchoolProfileTable;
        }
    }

}
